package com.wlz.rocketmq.delaymessage;

import com.wlz.rocketmq.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 *   延迟 消息
 * @author wlz
 * @date 2022-06-30  11:18 下午
 */
@Slf4j
public class DelayProducer {

    public static void main(String[] args) throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");

        producer.setNamesrvAddr(Constants.NAMESVADDR);

        producer.start();

        for (int i = 0; i < 2; i++) {

            try {
                Message msg = new Message("TopicTest", "TagA", ("hello world" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));

                // 设置延迟 等级
                //messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
                msg.setDelayTimeLevel(3);

                SendResult sendResult = producer.send(msg);

                log.info("sendResult: {}", sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }



        producer.shutdown();
        


    }
}
